package org.jeecg.modules.monitoring.j_monitoring.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.monitoring.j_monitoring.utils.EquipmentUtils;
import org.jeecg.modules.monitoring.j_monitoring.entity.AllSearch;
import org.jeecg.modules.monitoring.j_monitoring.mapper.AllSearchMapper;
import org.jeecg.modules.monitoring.j_monitoring.service.IAllSearchService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @Description: 全局设备
* @Author: xiaoyq
* @Date:   2023-01-11
* @Version: V1.0
*/
@Api(tags="全局设备")
@RestController
@RequestMapping("/j_monitoring/allSearch")
@Slf4j
public class AllSearchController extends JeecgController<AllSearch, IAllSearchService> {
   @Resource
   private AllSearchMapper allSearchMapper;

   /**
    * 分页列表查询
    *
    * @param allSearch
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @RequiresPermissions("全局设备:查询")
   @AutoLog(value = "全局设备-分页列表查询")
   @ApiOperation(value="全局设备-分页列表查询", notes="全局设备-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(AllSearch allSearch,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       Result<Page<AllSearch>> result=new Result<>();
       Page<AllSearch> pagelist = new Page(pageNo, pageSize);
       String ip = "";
       if(StringUtils.isNotEmpty(allSearch.getManageIp())){
           ip = allSearch.getManageIp();
           allSearch.setManageIp(null);
       }
       QueryWrapper<AllSearch> queryWrapper = QueryGenerator.initQueryWrapper(allSearch, req.getParameterMap());
       if(StringUtils.isNotEmpty(ip)){
           final String val = ip;
           queryWrapper.lambda().and(wrapper -> wrapper.or().or(w1->w1.like(AllSearch::getManageIp,val)));
       }

       pagelist=allSearchMapper.getPageList(pagelist,queryWrapper);
       List<AllSearch> networkResourceList  = pagelist.getRecords();
       for (AllSearch networkResource : networkResourceList){
           networkResource.setMonitorStatus(EquipmentUtils.getMonitorStatus(networkResource.getWhetherToMonitor()));
       }
       result.setCode(200);
       result.setSuccess(true);
       result.setResult(pagelist);
       return result;
   }

   /**
   * 导出excel
   *
   * @param request
   * @param allSearch
   */

   @RequiresPermissions("全局设备:导出")
   @RequestMapping(value = "/exportXls", method = RequestMethod.GET)
   @ApiOperation(value="全局设备-导出", notes="全局设备-导出")
   public ModelAndView exportXls(HttpServletRequest request, AllSearch allSearch) {
       return super.exportXls(request, allSearch, AllSearch.class, "全局搜索");
   }
}
